package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:03
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/ordersetting")
public class OrderSettingController {

    // dubbo消费者
    @Reference
    OrderSettingService orderSettingService;



    // 文件上传
    @RequestMapping(value = "/upload")
    public Result upload(@RequestParam(value = "excelFile") MultipartFile excelFile){
        try {
            // 1：使用POI解析文件，得到 List<String[]>（内容：2019/7/20	100）
            List<String[]> list = POIUtils.readExcel(excelFile);
            // 2：把List<String[]>，转换成List<OrderSetting>
            List<OrderSetting> orderSettingsList = new ArrayList<OrderSetting>();
            for (String[] str : list) {
                OrderSetting orderSetting = new OrderSetting(new Date(str[0]),Integer.parseInt(str[1]));
                orderSettingsList.add(orderSetting);
            }
            // 3：调用业务，进行保存
            orderSettingService.addOrderSetting(orderSettingsList);
            // 成功
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }

    // 使用当前月份，初始化当前月的预约设置信息
    @RequestMapping(value = "/initOrderSettingByDate")
    public Result initOrderSettingByDate(String date){
        try {
            /**
             *    map.put(“date”,1);
                 map.put(“number”,120);
                 map.put(“reservations”,10);
             */
            List<Map> list = orderSettingService.findOrderSettingByDate(date);
            // 成功
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    // 基于日历，实现预约设置，设置可预约人数number
    @RequestMapping(value = "/updateNumber")
    public Result updateNumber(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.updateNumber(orderSetting);
            // 成功
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
